var router = require('express').Router();
var util = require('util');
var uuid = require('node-uuid');
var EventProxy = require("eventproxy");
var cookie = require('cookie');
var cryptojs = require('cryptojs').Crypto;
var async = require('async');

var Consts = require('../consts');
var resultJson = require('../result-json');
var Utils = require('../utils');
var logger = require('../models/logger');
var User = require('../models/User');
var Course = require('../models/Course');
var Examine = require('../models/Examine');

//数据迁移
router.get("/copyData", function(req, res) {
	var account = req.query.account;
	var password = req.query.password;


	if (account == "admin_admin" && password == "paseeeeeesword_password") {
		console.log("认证成功");


		//连接job mongodb
		var mongodb = require('mongodb');
		var server = new mongodb.Server('182.92.221.113', '27017', {
			auto_reconnect: true
		});
		var db = new mongodb.Db('elearning', server, {

			safe: true
		});
		var jobConnect = null;

		//连接 oa mongodb
		console.log("oaConnect success");
		//连接mysql数据库


		async.series({
			examInfo: function(cbs) {
				examInfo(db, cbs)
			},

		}, function(err, results) {
			console.log("over")
		});



	} else
		console.log("认证失败");

	res.send(1);

})

function examInfo(db, cbs) {
	//同步就业信息
	console.log("insert exam  ...");
	db.open(function(err, db) {
		if (!err) {
			db.authenticate("elearning_ma", "BCHY_elearning", function(err, result) {
				db.createCollection('examines', {
					safe: true
				}, function(err, collection) {
					if (err)
						return logger.error(err);
					collection.find({

					}).toArray(function(err, docs) {
						if (err)
							return console.error(err);
						var docs = JSON.parse(JSON.stringify(docs));
						async.each(docs, function(item, cb) {
							Course.find({
								"chapter.knowledge.name": item.knowledge.name
							}, {
								"chapter.$.knowledge": 1
							}).exec(function(err, courses) {
								if (err)
									return console.error("insert exam err:", err);
								var ids = [];
								for (var i = 0; i < courses.length; i++) {
									for (var j = 0; j < courses[i].chapter.length; j++) {
										for (var m = 0; m < courses[i].chapter[j].knowledge.length; m++) {
											if (item.knowledge.name == courses[i].chapter[j].knowledge[m].name)
												ids.push(courses[i].chapter[j].knowledge[m]._id);
										};
									}
								};
								var list = [];
								for (var i = 0; i < item.que.answer.length; i++) {
									list.push({
											Id: uuid.v4(), //选项ID
											Content: item.que.answer[i].name, // 选项内容
											IfRightAnswer: item.que.answer[i].status, //选项是否正确答案
											OrderNum: i+1, //选项的序号
										})  
								};
								console.log(ids, ids.length);
								if (ids.length == 1) {
									new Examine({
										owner: ids[0], //知识点id
										Content: item.que.name, // 题目名称,题干
										QuestionType: 2, //题目类型(1单选、2多选、3判断、4填空、5问答、6连线（匹配）、7排序)
										Tip: item.que.answerTip, //答案解析
										QuestionOptionList: list //选项数组
									}).save(function(err){
										if(err)
											return console.error("insert exam err:", err);
										cb();
									})
								} else
									cb();
							});
						}, function(err) {
							if (err)
								return console.error("insert exam err:", err);
							console.log("insert exam success");
							cbs();
						})
					});

				});
			});
		}
	});
}
module.exports = router;